package com.ldj.struts.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.MenuBuilder;
import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.DataDictionary;
import com.ldj.hibernate.DataDictionaryDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.struts.form.DataDictionaryForm;

public class DataDictionaryAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		String code = request.getParameter("code");
		if(code !=null && !"".equals(code)){		
			if(code.equals("1")){
			   code = "";
		    }
			request.getSession().setAttribute("code", code);
		}
		code = (String)request.getSession().getAttribute("code");
		List<DataDictionary> list= (List<DataDictionary>)dao.findAllPage(code);
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("toAdd");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
		DataDictionary info = dao.findById(request.getParameter("did"));
		request.setAttribute("dInfo", info);
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DataDictionaryForm dForm = (DataDictionaryForm)form;
		DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
		DataDictionary info = dao.findById(request.getParameter("did"));
		info.setDataDictionaryDescription(dForm.getDataDictionaryDescription());
		info.setDataDictionaryName(dForm.getDataDictionaryName());
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "DataDictionary.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getDataDictionaryName()));
		logDao.save(s);
		return new ActionForward("/DataDictionary.do?method=list", true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DataDictionaryForm dForm = (DataDictionaryForm)form;
		DataDictionary info = new DataDictionary();

		DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
		
		String code = (String)request.getSession().getAttribute("code");
		
		List<DataDictionary> list= (List<DataDictionary>)dao.getSonData(code);
		
		String codeMax = code+"001";
		
		if(list !=null &&list.size()>0){
		  codeMax = ((DataDictionary)list.get(list.size()-1)).getDataDictionaryCode();
		  int intcodemax = Integer.parseInt(codeMax.substring(codeMax.length() - 3))+1;
		  if((""+intcodemax).length()%3==1){
			  codeMax = "00"+intcodemax;			  
		  }else if((""+intcodemax).length()%3==2){
			  codeMax = "0"+intcodemax;
		  }
		  codeMax = code + codeMax;
		}
		info.setDataDictionaryDescription(dForm.getDataDictionaryDescription());
		info.setDataDictionaryName(dForm.getDataDictionaryName());
		info.setDataDictionaryCode(codeMax);
		dao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "DataDictionary.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getDataDictionaryName()));
		logDao.save(s);
		return new ActionForward("/DataDictionary.do?method=list", true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
		DataDictionary info = dao.findById(request.getParameter("did"));
		List list = dao.findByCode(info.getDataDictionaryCode());
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				dao.delete((DataDictionary)list.get(i));
			}
		}
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "DataDictionary.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getDataDictionaryName()));
		logDao.save(s);
		return new ActionForward("/DataDictionary.do?method=list", true);
	}
	
	public ActionForward tree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
	//	String code = request.getParameter("code");
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("tree", bu.getDataDictionaryTree(request, response, "数据字典", "DataDictionary.do?method=list&code=1", "DataDictionary.do?method=list&code=$code"));		
		return mapping.findForward("tree");
	}

	
}